package test.integ.be.fedict.commons.eid.client;

import java.io.IOException;
import java.math.BigInteger;
import javax.smartcardio.Card;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import org.junit.Test;
import be.fedict.commons.eid.client.BeIDCardMachine;
import be.fedict.commons.eid.client.BeIDFileType;
import be.fedict.commons.eid.client.CardAndTerminalEventsManager;
import be.fedict.commons.eid.client.CardEventsListener;
import be.fedict.commons.eid.consumer.Identity;
import be.fedict.commons.eid.consumer.tlv.TlvParser;

public class BeIDCardMachineExercise {
	private BeIDCardMachine machine;

	@Test
	public void beidcardmachine() throws InterruptedException {
		//-------------------------------------------------------------------------------------------------------
		// instantiate a CardAndTerminalEventsManager with default settings (no logging, default timeout)
		//-------------------------------------------------------------------------------------------------------
		CardAndTerminalEventsManager cardAndTerminalEventsManager = new CardAndTerminalEventsManager();

		//-------------------------------------------------------------------------------------------------------	
		// register a CardEventsListener
		//-------------------------------------------------------------------------------------------------------
		cardAndTerminalEventsManager.addCardListener(new CardEventsListener() {
			@Override
			public void cardInserted(CardTerminal cardTerminal, Card card) {
				if (card != null) {
					System.err.println("Card ["
							+ String.format("%x", new BigInteger(1, card
									.getATR().getBytes()))
							+ "] Inserted Into Terminal ["
							+ cardTerminal.getName() + "]");
					machine = new BeIDCardMachine(card);

					try {
						byte[] idData = machine.readFile(BeIDFileType.Identity);
						Identity identity = TlvParser.parse(idData,
								Identity.class);
						System.out.println(identity.firstName + " "
								+ identity.name);
					} catch (CardException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} else {
					System.err.println("Card present but failed to connect()");
				}
			}

			@Override
			public void cardRemoved(CardTerminal cardTerminal) {
				System.err.println("Card Removed From ["
						+ cardTerminal.getName() + "]");
			}
		});

		//-------------------------------------------------------------------------------------------------------
		// start the CardAndTerminalEventsManager instance running as a daemon thread.
		//-------------------------------------------------------------------------------------------------------
		cardAndTerminalEventsManager.start();

		//-------------------------------------------------------------------------------------------------------
		// the main thread goes off and does other things (for this example, just loop and sleep)
		//-------------------------------------------------------------------------------------------------------
		System.err.println("Now.. insert and remove some cards");
		for (;;)
			Thread.sleep(2000);
	}
}
